package com.shuma.upms.permission.rpc;

import com.shuma.upms.permission.domain.entity.Result;
import com.shuma.upms.permission.service.PermissionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@Slf4j
@RequestMapping("/rpc/permission")
public class PermissionController {

    @Resource
    private PermissionService permissionService;

    // 绑定默认角色
    @PostMapping("/bind-default")
    public Result bindDefaultRole(@RequestParam("userId") Long userId) {
        //判空
        if (userId == null) {
            return Result.error("用户ID不能为空");
        }
        log.info("接收到为用户ID [{}] 绑定默认角色的请求。", userId);
        permissionService.bindDefaultRole(userId);
        return Result.success();
    }

    // 获取用户角色代码
    @GetMapping("/user-role-code")
    public Result<String> getUserRoleCode(@RequestParam("userId") Long userId) {
        //判空
        if (userId == null) {
            return Result.error("用户ID不能为空");
        }
        log.info("接收到查询用户ID {}角色码的请求。", userId);

        String roleCode = permissionService.getUserRoleCode(userId);

        if (roleCode == null) {
            return Result.error("未找到指定用户的角色信息");
        }

        return Result.success(roleCode);
    }

    // 升级为管理员
    @PostMapping("/upgrade-to-admin")
    public Result upgradeToAdmin(@RequestParam("userId") Long userId) {
        //判空
        if (userId == null) {
            return Result.error("用户ID不能为空");
        }
        log.info(" 接收到将用户ID [{}] 升级为管理员的请求。", userId);
        permissionService.upgradeToAdmin(userId);
        return Result.success();
    }

    // 降级为普通用户
    @PostMapping("/downgrade-to-user")
    public Result downgradeToUser( @RequestParam("userId") Long userId) {
        //判空
        if (userId == null) {
            return Result.error("用户ID不能为空");
        }
        log.info("接收到将用户ID [{}] 降级为普通用户的请求。", userId);
        permissionService.downgradeToUser(userId);
        return Result.success();
    }

}